<template>
  <el-row justify="end" align="middle">
    <el-space :size="10" spacer="|">
      <el-button size="small" type="primary" link @click="toggleDark">
        <Icon icon="svg-spinners:wind-toy"  style="color: #1a86f9" class="al-m-r-4 al-size-24"/>
        <el-text tag="b" type="primary" size="large">{{ mode }}</el-text>
      </el-button>
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          <Icon icon="ic:round-translate" style="color: #1a86f9" class="al-size-24"/>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="zh-cn">中文</el-dropdown-item>
            <el-dropdown-item command="en">English</el-dropdown-item>
            <el-dropdown-item command="ko">한글</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-space>
  </el-row>
  <el-row justify="center" style="overflow: hidden;">
    <el-col :span="12" style="height: calc(100vh - 36px);pointer-events: none;" class="hidden-sm-and-down al-relative">
      <el-image style="opacity: 0.15;position: absolute;z-index: -1;" class="al-w al-h" :src="svg_paintDreamUrl" fit="cover"/>
      <!-- 作为raw字符串传入 -->
      <!-- <div v-html="svgRobotRaw"></div> -->
      <!-- 作为组件传入 -->
      <!-- <svgRobotComponent /> -->
      <el-space wrap direction="vertical" alignment="start" :size="20" class="al-p-30">
        <div v-for="(skill, index) in skills" :key="skill.id" class="al-flex al-align-center">
          <strong class="al-size-32 al-m-r-20" :class="`textGrident-${(index+1)%4}`">
            {{ skill.title }}
          </strong>
          <span v-if="skill.id === 'Element-Plus'">
            <Icon :icon="skill.icon" class="al-size-40" />
            <span style="color:#409eff;vertical-align: top;" class="al-size-20">+</span>
          </span>
          <Icon v-else-if="skill.icon" :icon="skill.icon" class="al-size-40" />
          <el-image v-else style="height: 40px;width:40px;" :src="svg_chromajs" />
        </div>
      </el-space>
    </el-col>
    <el-col :span="12" style="position:relative;height: calc(100vh - 36px);">
      <el-image :src="svg_robotUrl" fit="cover" style="position: absolute;" class="al-w al-h animate__animated animate__bounceIn" />
      <!-- 'fill', 'contain', 'cover', 'none', 'scale-down' -->
      <el-image :src="logo" fit="fill" />
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="0px"
        status-icon
        class="al-m-40"
      >
        <el-form-item prop="account" class="animate__animated animate__bounceIn animate__delay-1s">
          <el-input v-model="ruleForm.account" 
            :prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password" class="animate__animated animate__bounceIn animate__delay-1s">
          <el-input
            v-model="ruleForm.password"
            type="password"
            placeholder="Please input password"
            show-password
            :prefix-icon="Lock"
          />
        </el-form-item>
        <el-form-item class="content-center animate__animated animate__bounceIn animate__delay">
          <el-button class="a-button" type="success" @click="submitForm(ruleFormRef)">
            {{ t('buttons.login') }}
          </el-button>
          <el-button class="a-button" type="primary" @click="resetForm(ruleFormRef)">
            {{ t('buttons.world') }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import {
  User,
  Lock
} from '@element-plus/icons-vue'

import { storeToRefs } from "pinia"
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from "vue-router"
import { useI18n } from 'vue-i18n'
import { useI18nLanguage, toggleDark } from '@/config'
import type { FormInstance, FormRules } from 'element-plus'
// import { useDark, useToggle, useColorMode, useCycleList } from '@vueuse/core'
import { useAppStore } from '@/stores/modules/global'
import { usePermissionStore } from '@/stores/modules/permission'
import { inject } from 'vue';

// 引入svg文件 - 作为url
import svg_robotUrl from '@/assets/svg/check-robot.svg?url'
import svg_paintDreamUrl from '@/assets/svg/概念插画信息.svg?url'
// 引入svg文件 - 作为字符串
// import svgRobotRaw from '@/assets/svg/check-robot.svg?raw'
// 引入svg文件 - 作为组件
// import svgRobotComponent from '@/assets/svg/check-robot.svg?component'

import svg_chromajs from '@/assets/svg/chroma.js.svg?url'
import { OPP } from '../world/docs'

const utils: any = inject('utils')
const logo = utils.getAssetsFile('assets/images/login/', 'algate', 'gif')

const skills = OPP.doc

const { t } = useI18n();

interface RuleForm {
  account: string
  password: string
}
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  account: 'algate',
  password: '123456'
})
const rules = reactive<FormRules<RuleForm>>({
  account: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 20, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
  password: [
    { required: true, message: 'Please input password', trigger: 'blur' },
  ]
})

// 登录
const router = useRouter()
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      const { isLogin, setLogin } = usePermissionStore()
      setLogin()
      isLogin && router.push({path: '/home', replace: true})
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  router.push({path: '/world'})
}

// 主题切换 dark light 
/* const isDark = useDark({
  selector: 'html',
  valueDark: 'dark',
  valueLight: 'light',
})
// 切换dark,light模式
const toggleDark = useToggle(isDark) */

// 'dark', 'light', 'gray', 'cafe'
/* const mode: any = useColorMode({
  emitAuto: true,
  modes: {
    gray: 'gray',
    cafe: 'cafe'
  }
})
// const arrStr = ['dark', 'light', 'gray', 'cafe']
const { state, next } = useCycleList([
  'dark',
  'light',
  'gray',
  'cafe'
])
const toggleDark = () => {
  const appStore = useAppStore()
  next()
  mode.value = state.value
  appStore.setMode(state.value)
  // mode.value = arrStr[Math.floor(Math.random() * arrStr.length)]
} */

// 切换语言
const appStore = useAppStore()
const { locale, mode } = storeToRefs(appStore)
const lang = ref(locale.value === 'zh-cn' ? true : false)
const route = useRoute()
const { changeTitle, setI18nLanguage } = useI18nLanguage()
const handleCommand = (command: string | number | object) => {
  changeLanguage(command)
}
const changeLanguage = (lang: any) => {
  setI18nLanguage(lang) // 也可以使用 store 来改变
  // locale.value = lang
  changeTitle(route.meta)
}
</script>
<style lang="scss" scoped>
.animate__animated.animate__bounceIn {
  --animate-duration: 2s;
}

.content-center {
  .el-form-item__content {
    align-content: center;
    .el-button {
      flex: 1;
    }
  }
}
:deep(.el-input__wrapper) {
  background-color: transparent;
  border-radius: unset;
}
:deep(.el-button).a-button {
  opacity: 0.8;
  border-radius: unset;
}
:deep(.el-space) > span {
  color: var(--el-color-info-light-3);
}
</style>
